﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using Barcode.DataMatrix;
using System.Drawing;
using System.Drawing .Imaging ;
using System.Runtime.InteropServices;



namespace MagicCube_v2_Demo
{
    public class DataMatrix
    {
        public Bitmap bmp;
        public string Header = "";
        //string txt;
        public char[] cpara;
        public char[] data;
        public byte[] param2;
        int iPixel = 4;   //可选值4,5,7

        //引用dll文件
        [DllImport(@"D:\com\barcode\DataMatrix.dll", EntryPoint = "dataMatrixEncode", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
        public static extern int dataMatrixEncode(string barcode, int barsize, ref byte codedstr);
        [DllImport(@"D:\com\barcode\DataMatrix.dll", EntryPoint = "dataMatrixEncode1", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]

        public static extern int dataMatrixEncode1(string barcode, int barsize, ref byte Dstr, int codedLen);


        public void Create(string codestr)
        {
            param2 = new byte[512];     //新建字节数组
            int result = dataMatrixEncode(codestr, 0, ref param2[0]);

            int n = result;
            //string str2 = "";
            //for (int i = 0; i < n; i++)
            //{
            //    str2 += param2[i].ToString();
            //    if (i % 10 == 0)
            //    {
            //        str2 += "\r\n";
            //    }
            //}
            cpara = System.Text.Encoding.Default.GetChars(param2);
            bmp =(Bitmap)RenderToBitmap(n,iPixel,cpara);
            //string tempName = "h" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
            //bmp.Save("//tmpimg//" + tempName + ".bmp", ImageFormat.Bmp);
        }

        //对一个0,1矩阵根据NoOfBlocksPerAxis（块的个数），每个小块的2像素的大小生成一个位图
        private Bitmap RenderToBitmap(int NoOfBlocksPerAxis, int pixel, char[] Data)
        {
            int n = NoOfBlocksPerAxis;
            Bitmap bmp = new Bitmap((NoOfBlocksPerAxis + 2) * pixel, (NoOfBlocksPerAxis + 2) * pixel);
            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(Brushes.White, 0, 0, (n + 2) * pixel, (n + 2) * pixel);
            for (int i = 0; i < Data.Length; i++)
            {
                if (Data[i] == '\0')
                    g.FillRectangle(Brushes.White, (i % n + 1) * pixel, (i / n + 1) * pixel, pixel, pixel);
                else
                    g.FillRectangle(Brushes.Black, (i % n + 1) * pixel, (i / n + 1) * pixel, pixel, pixel);
            }

            g.Dispose ();
            return bmp;
        }

        //将0，1字符串存在一个一维数组中方便使用
        public char[] GetData(string str)
        {
            string [] split = Header .Split (new char []{' ','x'});
            int n = Convert .ToInt32 (split [0]);
            data = new char[n*n];
            int j = 0;
            for(int i = 0;i<str .Length ;i++)
            {
                if(str[i]=='\r')
                    continue ;
                else
                    if(str[i] == '\n')
                        continue ;
                    else
                    {
                        data[j] = str[i];
                        j++;
                    }
            }
            return data;
        }

        //对字符串进行预处理，去掉例如16x16z这样的字符串
        private string pretreatmentOfString(string str)
        {
            string str1 = "";
            str1 = str.Substring (Header.Length);
            return str1;
        }


    }
}
